package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationEs;
import cn.wolfcode.luowowo.search.domain.StrategyEs;
import cn.wolfcode.luowowo.search.domain.TravelEs;
import cn.wolfcode.luowowo.search.domain.UserInfoEs;
import cn.wolfcode.luowowo.search.service.IDestinationEsService;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.service.ITravelEsService;
import cn.wolfcode.luowowo.search.service.IUserInfoEsService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 首页ES搜索
 * 折腾ES时需要初始化(从mongodb中把数据导入到Es中),这里是等用户发起请求时初始化
 */
@RestController
public class DataController {


    //Mongodb的服务
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;

    // Es的服务
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private IDestinationEsService destinationEsService;

    // @ApiIgnore:有些接口不想显示，就贴上去，可以贴在类上(controller都不显示)，也可以贴在方法上。(开发文档就不显示此接口了)
    @ApiIgnore
    @GetMapping("/dataInit")
    public Object dataInit(){
        System.err.println("你特么进来没有");
        // 把攻略数据从Mongodb数据库中取出来,放进Es库里
        List<Strategy> sts = strategyService.listAll();
        // 每一个攻略对象
        for (Strategy st : sts) {
            // 通过批量复制到ES数据库里
            StrategyEs es = new StrategyEs();
            // 参数1:被复制对象 参数2:目标对象
            BeanUtils.copyProperties(st, es);
            // 保存到Es数据库中
            strategyEsService.save(es);
        }

        // 目的地(同上)
        List<Destination> dests = destinationService.listAll();
        for (Destination d : dests) {
            DestinationEs es = new DestinationEs();
            BeanUtils.copyProperties(d,es);
            destinationEsService.save(es);
        }

        // 游记
        List<Travel> ts = travelService.listAll();
        for (Travel t : ts) {
            TravelEs es = new TravelEs();
            BeanUtils.copyProperties(t,es);
            travelEsService.save(es);
        }
        // 用户
        List<UserInfo> uf = userInfoService.listAll();
        for (UserInfo u : uf) {
            // Es对象
            UserInfoEs es = new UserInfoEs();
            BeanUtils.copyProperties(u,es);
            userInfoEsService.save(es);
        }
        return "ok";
    }
}
